package io.renren.modules.sys.controller;

import cn.hutool.db.Page;
import io.renren.common.interceptor.UserContext;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.modules.sp.dto.SpUserDTO;
import io.renren.modules.sqlapi.service.SysApiSqlService;
import io.renren.modules.sys.dao.SysCommDao;
import io.renren.modules.sys.dto.SysRoleDTO;
import io.swagger.annotations.Api;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/front/fast")
@Api(tags="用户管理")
public class FastController {
    @Autowired
    private SysCommDao sysCommDao;
    @Autowired
    private SysApiSqlService sysApiSqlService;

    @GetMapping("/{model}/{code}/list")
    public Result< List<Map<String, String>> > list(@RequestParam Map<String, Object> params,  @PathVariable("model") String model,@PathVariable("code") String code){
        initParam(params);
        List<Map<String, String>> list = sysApiSqlService.list(code,params);
        return new Result< List<Map<String, String>> >().ok(list);
    }
    @GetMapping("/{model}/{code}/page")
    public Result<PageData<Map>> page(@RequestParam Map<String, Object> params,Page page, @PathVariable("model") String model, @PathVariable("code") String code) {
        initParam(params);
        //特殊处理逻辑
        if ("match".equals(code)) {
            SpUserDTO spUserDTO = UserContext.getUser();
            //如果是新疆建设兵团的660000，改为650000
            if ("660000".equals(spUserDTO.getProvince())) {
                spUserDTO.setProvince("650000");
            }
        }
        PageData<Map> pageData = sysApiSqlService.pageData(page, code, params);
        return new Result<PageData<Map>>().ok(pageData);
    }
    @GetMapping("/{model}/{code}/info")
    public Result<Map> info( @RequestParam Map<String, Object> params,@PathVariable("model") String model,@PathVariable("code") String code){
        initParam(params);
        List<Map<String, String>> list = sysApiSqlService.list(code,params);
        if(list.size()==0){
           return new  Result<Map>().ok(new HashMap<>());
        }

        return new Result<Map>().ok(list.get(0));
    }
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
    public void initParam( Map<String, Object> params){
        for(String key : params.keySet()){
            String v = (String)params.get(key);
            if(StringUtils.isNotBlank(v)&&key.contains("Date")){
                try {
                    Date data = DateUtils.parseDate(v.substring(0,10),parsePatterns);
                    params.put(key,data);
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
        params.put("user", UserContext.getUser());
    }
}
